package com.gljx.web.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.CommonExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.TrainChargeParament;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.TrainChargeParamentService;

@Controller
@RequestMapping("/")
public class TrainChargeParamentrController {

	//自动注入
	@Autowired
	private TrainChargeParamentService trainChargeParamentService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;
	
	//自动注入
	@Autowired
	private CustomLogService customLogService;
	
	
	/**
	 * 查询财务自定义收费项目
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryTrainChargeParamentr/look.action")
    public String queryTrainChargeParamentr(HttpServletRequest request, String flag) {
        //获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//根据公司id查询
		List<TrainChargeParament> trainChargeParaments = trainChargeParamentService.queryTrainChargeParamentByCompangId(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("trainChargeParaments", trainChargeParaments);
        request.setAttribute("flag", flag);
        return "/backHome/paramentData/trainChargeParaments/list";
	}
	
	/**
	 * 根据名称查询财务自定义收费项目
	 * @param request
	 * @param fnameL2
	 * @return
	 */
	@RequestMapping("/queryTrainChargeParamentrFnameL2")
	public String queryTrainChargeParamentrFnameL2(HttpServletRequest request,String fnameL2){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
        //去除空格
        fnameL2 = fnameL2.trim();
        //根据名称查询
		List<TrainChargeParament> trainChargeParaments = trainChargeParamentService.selectTrainChargeParamentByCompangIdAndName(user.getFcompanyid(), fnameL2);
		//把数据传到页面
		request.setAttribute("trainChargeParaments", trainChargeParaments);
		request.setAttribute("fnameL2", fnameL2);
		return "/backHome/paramentData/trainChargeParaments/list";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertTrainChargeParamentr")
	public String insertTrainChargeParamentr(HttpServletRequest request,String type){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
        /*String number = customLogService.selectFunmber("ct_ds_trainchargeparament", user.getFcompanyid());*/
	    //根据上一个编码获取编码
        String fnumber = GetFnumber.fnumbers("CWZDYSFXM");
	    //获取组织
	  	Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  	//把数据传到页面
		request.setAttribute("department", department);
	  	request.setAttribute("fnumber", fnumber);
	  	request.setAttribute("type", type);
		return "/backHome/paramentData/trainChargeParaments/edit";
	}
	
	/**
	 * 添加，修改财务自定义收费项目
	 * @param request
	 * @param trainChargeParamentr
	 * @param fid
	 * @return
	 */
	@RequestMapping("/editTrainChargeParamentr")
	public String editTrainChargeParamentr(HttpServletRequest request,TrainChargeParament trainChargeParamentr,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
	    //公司id
		trainChargeParamentr.setCfficompanyid(user.getFcompanyid());
		//控制单元id
		trainChargeParamentr.setFcontrolunitid(user.getFcompanyid());
	    //获取当前时间
	  	String time = TimeFormat.timeFormat(new Date());
	  	if(fid == null || "".equals(fid)){
	  		//利用UUID生成fid
			trainChargeParamentr.setFid(GetFid.UUIDIntoFid());
	  		//当前创建时间
		  	trainChargeParamentr.setFcreatetime(time);
		   //创建人id
		   trainChargeParamentr.setFcreatorid(user.getFid());
		   //完成添加
		   trainChargeParamentService.add(trainChargeParamentr);
		   //添加日志
		   customLogService.addLog("财务基础资料", time+"对财务自定义收费项目进行添加操作", null, user.getFid());
		    //重定向到添加方法
		    return "redirect:/DrivingSys/insertTrainChargeParamentr?type=3";
	  	}else{
	  		//修改时间
	  		trainChargeParamentr.setFlastupdatetime(time);
	  		//修改人
	  		trainChargeParamentr.setFlastupdateuserid(user.getFid());
	  		//修改
	  		trainChargeParamentService.modify(trainChargeParamentr);
	  		//添加日志
			customLogService.addLog("财务基础资料", time+"对财务自定义收费项目进行修改操作", null, user.getFid());
		    //重定向到查询页面
            return "redirect:/DrivingSys/queryTrainChargeParamentr/look.action?flag=3";
        }
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid
	 * @return
	 */
	@RequestMapping("/selectTrainChargeParamentrByFid")
	public String selectTrainChargeParamentrByFid(HttpServletRequest request,String fid){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取组织
	  	Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  	//根据fid查询
	  	TrainChargeParament trainChargeParament = trainChargeParamentService.queryTrainChargeParamentByFid(fid);
	  	//把数据传到页面
	  	request.setAttribute("department", department);
	  	request.setAttribute("trainChargeParament", trainChargeParament);
		return "/backHome/paramentData/trainChargeParaments/edit";
	}
	
	/**
	 * 根据fid删除
	 * @param request
	 * @param deleteId
	 * @param fid
	 * @return
	 */
	@RequestMapping("/deleteTrainChargeParamentrByFid")
	public String deleteTrainChargeParamentrByFid(HttpServletRequest request,String[] deleteId,String fid){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //删除
	    trainChargeParamentService.deleteByFid(deleteId,fid);
	    //添加日志
	    customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date())+"对财务自定义收费项目进行删除操作", null, user.getFid());
		//重定向到查询页面
	    return "redirect:/DrivingSys/queryTrainChargeParamentr/look.action";
	}

	/**
	 * 导出Excel
	 *
	 * @param response
	 * @param request
	 * @param fnameL2  名称
	 */
	@RequestMapping("/importTrainChargeParamentr")
	public void importTrainChargeParamentr(HttpServletResponse response, HttpServletRequest request, String fnameL2) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		//定义接收类型
		List<Object> data = new ArrayList<>();
		//查询数据
        List<TrainChargeParament> trainChargeParaments = trainChargeParamentService.selectTrainChargeParamentByCompangIdAndName(user.getFcompanyid(), fnameL2.trim());
        for (TrainChargeParament trainChargeParament : trainChargeParaments) {
			data.add(trainChargeParament.getFnumber());
			data.add(trainChargeParament.getFnameL2());
			data.add(trainChargeParament.getFsimplename());
			data.add(trainChargeParament.getFdescriptionL2());
			data.add(trainChargeParament.getCompanyName());
			data.add(trainChargeParament.getFcreatetime());
			data.add(trainChargeParament.getFlastupdatetime());
			data.add(trainChargeParament.getCreateName());
			data.add(trainChargeParament.getUpdateName());
			data.add(trainChargeParament.getCfamount());
			if (trainChargeParament.getCfstate() == null) {
				data.add(null);
			} else if (trainChargeParament.getCfstate().compareTo(new BigDecimal("1")) == 0) {
				data.add("启用");
			} else {
				data.add("禁用");
			}
		}
		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "驾校", "创建时间", "最后修改时间", "创建者", "修改人", "金额", "状态"};
		//长度
		Integer length[] = {9000, 5000, 5000, 5000, 7000, 5000, 5000, 3000, 3000, 2000, 2000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "财务自定义收费项目" + TimeFormat.getNowTime2() + ".xls", "财务自定义收费项目", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
